package com.seh.contentcenter.controller;

import com.seh.contentcenter.domain.Result;
import com.seh.contentcenter.dto.OrderDTO;
import com.seh.contentcenter.entity.Order;
import com.seh.contentcenter.service.Impl.OrderServiceImpl;
import com.seh.contentcenter.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;

    /**
     * 新增订单
     *
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public String addOrder(@RequestBody Order order) {
        String result = orderService.addOrder(order);
        return result;
    }

    /**
     * 根据用户ID查询所有订单
     *
     * @param userId
     * @return
     */
    @GetMapping("/findAllOrdersByUserId/{userId}")
    public Result<List<OrderDTO>> findAllOrdersByUserId(@PathVariable Integer userId) {
        List<OrderDTO> allOrderByUserId = orderService.findAllOrderByUserId(userId);
        return ResultUtil.orderInfo(allOrderByUserId);
    }

    /**
     * 根据主键id，查看订单的详细信息
     *
     * @param orderId
     * @return
     */
    @GetMapping("/selectOrderById/{orderId}")
    public Result<OrderDTO> selectOrderById(@PathVariable Integer orderId) {
        OrderDTO orderDTO = orderService.selectOrderById(orderId);
        return ResultUtil.orderInfo(orderDTO);
    }

    /**
     * 逻辑删除
     *
     * @param orderId
     * @return
     */
    @GetMapping("/deleteOrderById/{orderId}")
    public String deleteOrderById(@PathVariable Integer orderId) {
        String result = orderService.deleteOrderById(orderId);
        return result;
    }

    /**
     * 用户中心：我买到的
     * 通过下单用户id和订单状态，获取购买到的商品
     * @param sellerId
     *
     *
     * @return
     */
    @GetMapping("/findAllOrderByUserIdAndOrderStatus")
    public Result<List<OrderDTO>> findAllOrderByUserIdAndOrderStatus(@RequestParam("sellerId") Integer sellerId) {
        return ResultUtil.orderInfo(orderService.findAllOrderByUserIdAndOrderStatus(sellerId));
    }

    /**
     * 用户中心：我卖出的
     * 通过卖家id查询
     *
     * @param sellerId
     * @return
     */
    @GetMapping("/findAllOrderByUserHasSold/{sellerId}")
    public List<OrderDTO> findAllOrderByUserHasSold(@PathVariable Integer sellerId) {
        List<OrderDTO> allOrderByUserHasSold = orderService.findAllOrderByUserHasSold(sellerId);
        return allOrderByUserHasSold;
    }

    /**
     * 用户购买之后，更新订单的状态
     * @param order
     * @return
     */
    @PostMapping("/updateOrderStatus")
    public Result<OrderDTO> updateOrderStatus(@RequestBody Order order) {
        return ResultUtil.orderInfo(orderService.updateOrderStatus(order));
    }

    /**
     * 获取所有<我买到的>
     * @param userId
     * @param pageIndex
     * @return
     */
    @GetMapping("/findAllOrderByBought")
    public Result<List<OrderDTO>> findAllOrderByBought(@RequestParam("userId") Integer userId,
                                                       @RequestParam("pageIndex") Integer pageIndex) {
        return ResultUtil.orderInfo(orderService.findAllOrderByBought(userId, pageIndex));
    }

    /**
     * 收货成功，修改订单信息
     * @param orderId
     * @return
     */
    @GetMapping("/updateOrderDone/{orderId}")
    public String updateOrderDone(@PathVariable Integer orderId) {
        return orderService.updateOrderDone(orderId);
    }

    /**
     * 若支付失败，则将订单状态修改为：0
     * @param orderId
     * @return
     */
    @GetMapping("/updateOrderStatus/{orderId}")
    public String updateOrderStatus(@PathVariable Integer orderId) {
        return orderService.updateOrderStatus(orderId);
    }
}
